package ch13;

import java.util.ArrayList;

// ATM.java
// Represents an automated teller machine

public class ATM 
{
   private boolean userAuthenticated; // whether user is authenticated
   private int currentAccountNumber; // current user's account number
   private Screen screen; // ATM's screen
   private Keypad keypad; // ATM's keypad
   private CashDispenser cashDispenser; // ATM's cash dispenser
   private DepositSlot depositSlot; // ATM's deposit slot
   private BankDatabase bankDatabase; // account information database

   // constants corresponding to main menu options
   private static final int BALANCE_INQUIRY = 1;
   private static final int WITHDRAWAL = 2;
   private static final int DEPOSIT = 3;
   private static final int EXIT = 4;

   // irrelevant stringarray for menu-visualization
   private ArrayList<String> stringArray = new ArrayList<String>();
   // no-argument ATM constructor initializes instance variables
   public ATM() 
   {
      userAuthenticated = false; // user is not authenticated to start
      currentAccountNumber = 0; // no current account number to start
      screen = new Screen(); // create screen
      keypad = new Keypad(); // create keypad 
      cashDispenser = new CashDispenser(); // create cash dispenser
      depositSlot = new DepositSlot(); // create deposit slot
      bankDatabase = new BankDatabase(); // create acct info database
   } // end no-argument ATM constructor

   // start ATM 
   public void run()
   {
      // welcome and authenticate user; perform transactions
      while ( true )
      {
         // loop while user is not yet authenticated
         while ( !userAuthenticated ) 
         {	
        	 screen.printHelper("              db   888888888888 88b           d88\n", 1); 
        	 screen.printHelper("             d88b       88      888b         d888\n", 1);
        	 screen.printHelper("            d8'`8b      88      88`8b       d8'88\n", 1);
        	 screen.printHelper("           d8'  `8b     88      88 `8b     d8' 88\n", 1);
        	 screen.printHelper("          d8YaaaaY8b    88      88  `8b   d8'  88\n", 1);
        	 screen.printHelper("         d8        8b   88      88   `8b d8'   88\n", 1);
        	 screen.printHelper("        d8'        `8b  88      88    `888'    88\n", 1);
        	 screen.printHelper("       d8'          `8b 88      88     `8'     88\n", 1);
        
        	 
        	 
        	 
// dette gor jeg for at hvis systemet kommer tilbage til start saa tager den ikke gammel kode med    	 
        stringArray.clear();
        stringArray.add("  ");
        stringArray.add("  ");
        stringArray.add("Welcome!");
        stringArray.add("  ");
        stringArray.add("Please Enter your account number");
        stringArray.add("  ");
        stringArray.add("  ");
        screen.setKasse(stringArray);	 

// Saadan kunne det have set ud , men her er der meget mere kode og meget mere manuelt arbejde
//        	screen.printHelper("+", 1);// start
//        	screen.printHelper("-", 60);
//        	screen.printHelper("+\n", 1);
//        	screen.printHelper("|", 1);
//        	screen.printHelper(" ", 60);
//        	screen.printHelper("|\n", 1);
//        	screen.printHelper("|", 1);
//        	screen.printHelper(" ", 60);
//        	screen.printHelper("|\n", 1);
//        	screen.printHelper("|", 1);
//        	screen.printHelper(" ", 60);
//        	screen.printHelper("|\n", 1);
//        	screen.printHelper("|", 1);
//        	screen.printHelper(" ", 60);
//        	screen.printHelper("|\n", 1);
//        	screen.printHelper("|", 1);
//        	screen.printHelper(" ", 60);
//        	screen.printHelper("|\n", 1);
//        	screen.printHelper("|", 1);
//        	screen.printHelper(" ", 60);
//        	screen.printHelper("|\n", 1);
//        	screen.printHelper("|", 1);
//        	screen.printHelper(" ", 60);
//        	screen.printHelper("|\n", 1);
//        	screen.printHelper("|", 1);
//        	screen.printHelper(" ", spaces);
//        	screen.printHelper(str, 1);
//        	screen.printHelper(" ", spaces);
//        	screen.printHelper("|\n", 1);
//        	screen.printHelper("|", 1);//midt
//        	screen.printHelper(" ", 60);
//        	screen.printHelper("|\n", 1);
//        	screen.printHelper("|", 1);
//        	screen.printHelper(" ", 60);
//        	screen.printHelper("|\n", 1);
//        	screen.printHelper("|", 1);
//        	screen.printHelper(" ", 60);
//        	screen.printHelper("|\n", 1);
//        	screen.printHelper("|", 1);
//        	screen.printHelper(" ", 60);
//        	screen.printHelper("|\n", 1);
//        	screen.printHelper("|", 1);
//        	screen.printHelper(" ", 60);
//        	screen.printHelper("|\n", 1);
//        	screen.printHelper("|", 1);
//        	screen.printHelper(" ", 60);
//        	screen.printHelper("|\n", 1);
//        	screen.printHelper("+", 1);
//        	screen.printHelper("-", 60);
//        	screen.printHelper("+\n", 1);//slut
            
            authenticateUser(); // authenticate user
         } // end while
         
         performTransactions(); // user is now authenticated 
         userAuthenticated = false; // reset before next ATM session
         currentAccountNumber = 0; // reset before next ATM session 
         
         stringArray.clear();
         stringArray.add("\nThank you! Goodbye!");
         screen.setKasse(stringArray);
         
      } // end while   
   } // end method run

   // attempts to authenticate user against database
   private void authenticateUser() 
   {
	  
     
      int accountNumber = keypad.getInput(); // input account number
      
      
      stringArray.clear();
      stringArray.add("Enter your PIN");
      screen.setKasse(stringArray);
      
      
      // prompt for PIN
      int pin = keypad.getInput(); // input PIN
      
      // set userAuthenticated to boolean value returned by database
      userAuthenticated = 
         bankDatabase.authenticateUser( accountNumber, pin );
      
      // check whether authentication succeeded
      if ( userAuthenticated )
      {
         currentAccountNumber = accountNumber; // save user's account #
      } // end if
      else{
    	  
    	  stringArray.clear();
          stringArray.add("Invalid account number or PIN. Please try again.");
          screen.setKasse(stringArray);}

   } // end method authenticateUser

   // display the main menu and perform transactions
   private void performTransactions() 
   {
      // local variable to store transaction currently being processed
      Transaction currentTransaction = null;
      
      boolean userExited = false; // user has not chosen to exit

      // loop while user has not chosen option to exit system
      while ( !userExited )
      {     
         // show main menu and get user selection
         int mainMenuSelection = displayMainMenu();

         // decide how to proceed based on user's menu selection
         switch ( mainMenuSelection )
         {
            // user chose to perform one of three transaction types
            case BALANCE_INQUIRY: 
            case WITHDRAWAL: 
            case DEPOSIT:

               // initialize as new object of chosen type
               currentTransaction = 
                  createTransaction( mainMenuSelection );

               currentTransaction.execute(); // execute transaction
               break; 
            case EXIT: // user chose to terminate session
            	
            	 stringArray.clear(); 
                 stringArray.add("Exiting the system...");
                 screen.setKasse(stringArray);
              
               userExited = true; // this ATM session should end
               break;
            default: // user did not enter an integer from 1-4
               
            	stringArray.clear(); 
                stringArray.add("\nYou did not enter a valid selection. Try again.");
                screen.setKasse(stringArray);
            
               break;
         } // end switch
      } // end while
   } // end method performTransactions
   
   // display the main menu and return an input selection
   private int displayMainMenu()
   {
	   
	   stringArray.clear();
	   stringArray.add("Main menu:");
	   stringArray.add("1 - View my balance ");
	   stringArray.add("2 - Withdraw cash ");
	   stringArray.add("3 - Deposit funds ");
       stringArray.add("4 - Exit");
       stringArray.add("  ");
       stringArray.add("Enter a choice");
       screen.setKasse(stringArray);
	   
	   
      return keypad.getInput(); // return user's selection
   } // end method displayMainMenu
         
   // return object of specified Transaction subclass
   private Transaction createTransaction( int type )
   {
      Transaction temp = null; // temporary Transaction variable
      
      // determine which type of Transaction to create     
      switch ( type )
      {
         case BALANCE_INQUIRY: // create new BalanceInquiry transaction
            temp = new BalanceInquiry( 
               currentAccountNumber, screen, bankDatabase );
            break;
         case WITHDRAWAL: // create new Withdrawal transaction
            temp = new Withdrawal( currentAccountNumber, screen, 
               bankDatabase, keypad, cashDispenser );
            break; 
         case DEPOSIT: // create new Deposit transaction
            temp = new Deposit( currentAccountNumber, screen, 
               bankDatabase, keypad, depositSlot );
            break;
      } // end switch

      return temp; // return the newly created object
   } // end method createTransaction
} // end class ATM



/**************************************************************************
 * (C) Copyright 1992-2012 by Deitel & Associates, Inc. and               *
 * Pearson Education, Inc. All Rights Reserved.                           *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/